package com.flyqiu.flow.api.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyqiu.flow.model.entity.Comment;
import com.flyqiu.flow.model.entity.base.Task;
import com.flyqiu.flow.model.entity.hi.ProcHiActivity;
import com.flyqiu.flow.model.entity.hi.ProcHiInst;
import com.flyqiu.flow.model.entity.hi.ProcHiUserTask;
import com.flyqiu.flow.model.entity.ru.ProcRuActivity;

import java.util.List;

/**
 * 历史服务
 */
public interface FlowHistoryService {
    /**
     * 根据流程实例id 获取流程实例
     *
     * @param procInstId
     * @return
     */
    ProcHiInst getProcInst(String procInstId);

    /**
     * 分页查询流程实例
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    Page<ProcHiInst> pageProcInst(Page<ProcHiInst> page, LambdaQueryWrapper<ProcHiInst> queryWrapper);

    /**
     * 列表查询流程实例
     *
     * @param queryWrapper
     * @return
     */
    List<ProcHiInst> listProcInst(LambdaQueryWrapper<ProcHiInst> queryWrapper);

    /**
     * 查询流程实例数量
     *
     * @param queryWrapper
     * @return
     */
    long countProcInst(LambdaQueryWrapper<ProcHiInst> queryWrapper);

    /**
     * 根据任务id获取任务
     *
     * @param taskInstId
     * @return
     */
    ProcHiUserTask getTask(String taskInstId);

    /**
     * 分页查询用户任务
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    Page<ProcHiUserTask> pageTask(Page<ProcHiUserTask> page, LambdaQueryWrapper<ProcHiUserTask> queryWrapper);

    /**
     * 列表查询用户任务
     *
     * @param queryWrapper
     * @return
     */
    List<ProcHiUserTask> listTask(LambdaQueryWrapper<ProcHiUserTask> queryWrapper);

    /**
     * 根据流程实例id 获取任务
     *
     * @param procInstId
     * @return
     */
    default List<ProcHiUserTask> listTaskByProcInstId(String procInstId) {
        LambdaQueryWrapper<ProcHiUserTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Task::getProcInstId, procInstId);
        lambdaQueryWrapper.orderByDesc(Task::getCreateTime);
        return listTask(lambdaQueryWrapper);
    }

    /**
     * 查询任务数量
     *
     * @param queryWrapper
     * @return
     */
    long countTask(LambdaQueryWrapper<ProcHiUserTask> queryWrapper);

    /**
     * 根据活动id获取活动
     *
     * @param activityId
     * @return
     */
    ProcHiActivity getActivity(String activityId);

    /**
     * 分页查询活动
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    Page<ProcHiActivity> pageActivity(Page<ProcHiActivity> page, LambdaQueryWrapper<ProcHiActivity> queryWrapper);

    /**
     * 列表查询流程活动
     *
     * @param queryWrapper
     * @return
     */
    List<ProcHiActivity> listActivity(LambdaQueryWrapper<ProcHiActivity> queryWrapper);

    /**
     * 根据流程实例id查询活动实例
     *
     * @param procInstId
     * @return
     */
    default List<ProcHiActivity> listActivityByProcInstId(String procInstId) {
        LambdaQueryWrapper<ProcHiActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcRuActivity::getProcInstId, procInstId);
        return listActivity(queryWrapper);
    }

    /**
     * 获取流程活动数量
     *
     * @param queryWrapper
     * @return
     */
    long countActivity(LambdaQueryWrapper<ProcHiActivity> queryWrapper);

    /**
     * 分页查询备注信息
     *
     * @param page
     * @param lambdaQueryWrapper
     * @return
     */
    Page<Comment> pageComment(Page<Comment> page, LambdaQueryWrapper<Comment> lambdaQueryWrapper);

    /**
     * 列表查询备注信息
     *
     * @param lambdaQueryWrapper
     * @return
     */
    List<Comment> listComment(LambdaQueryWrapper<Comment> lambdaQueryWrapper);

    /**
     * 删除备注信息
     *
     * @param queryWrapper
     */
    void deleteComment(LambdaQueryWrapper<Comment> queryWrapper);

    /**
     * 根据流程实例查询备注信息
     *
     * @param procInstId
     * @return
     */
    default List<Comment> listCommentByProcInstId(String procInstId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getProcInstId, procInstId);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        return listComment(queryWrapper);
    }

    /**
     * 获取备注数量
     *
     * @param lambdaQueryWrapper
     * @return
     */
    long countComment(LambdaQueryWrapper<Comment> lambdaQueryWrapper);

    default void deleteProcInst(String procInstId) {
        deleteProcInst(procInstId, null, null);
    }

    default void deleteProcInst(String procInstId, JSONObject params) {
        deleteProcInst(procInstId, params, null);
    }

    default void deleteProcInst(String procInstId, String reason) {
        deleteProcInst(procInstId, null, reason);
    }

    void deleteProcInst(String procInstId, JSONObject params, String reason);
}
